<template>
  <main-comp :alternate="alternate" v-if="show">
    <div class="m-mail-open" :style="mailAni">
      <div class="paper-back"></div>
      <component :is="scene" :style="animation(animateName, 1, 0.6)"/>
    </div>
  </main-comp>
</template>

<script>
/* eslint-disable */
import mainComp from '../comps/secondary-comp'
import oneComp from './sub/one'
import twoComp from './sub/two'
import threeComp from './sub/three'
import fourComp from './sub/four'
import fiveComp from './sub/five'
import sixComp from './sub/six'
import { mapState } from 'vuex'
const scenes = [oneComp, twoComp, threeComp, fourComp, fiveComp, sixComp]

export default {
  components: {
    mainComp
  },
  data() {
    return {
      indexScene: 0,
      alternate: false,
      show: true,
      direction: 1
    }
  },
  computed: {
    ...mapState({
      up: state => state.up,
      down: state => state.down
    }),
    scene() {
      return scenes[this.indexScene]
    },
    mailAni() {
      return this.alternate ? this.animation('fadeOut', 1, 1.1, 0) : this.animation('fadeIn', 1, 0.6, 0.6)
    },
    animateName() {
      switch(this.direction) {
        case 0:
          return 'fadeInUp'
        case 1:
          return 'fadeInDown'
        case 2:
          return 'fadeOutUp'
        case 3:
          return 'fadeOutDown'
      }
    }
  },
  methods: {
    animateCB (cb) {
      this.show = false
      this.$nextTick(() => {
        this.show = true
        this.alternate = true
        setTimeout(() => {
          cb && cb()
        }, 900)
      })
    }
  },
  watch: {
    up(n) {
      if (n) {
        this.direction = 2
        setTimeout(() => {
          this.direction = 1
          this.indexScene ++;
          if (this.indexScene >= scenes.length) {
            this.animateCB(() => {
              this.$store.dispatch('nextScene')
            })
          }
        }, 600)
      }
    },
    down(n) {
      if (n) {
        if (this.indexScene === 0) {
          this.animateCB(() => {
            this.$store.dispatch('prevScene')
          })
          return
        };
        this.direction = 3
        setTimeout(() => {
          this.direction = 0
          if (this.indexScene > 0)this.indexScene --;
        }, 600)
      }
    }
  }
}
</script>

<style scoped>
.m-mail-open {
  background: url('../assets/mail_open.png') no-repeat;
  background-size: 100% 100%;
  position: absolute;
  width: 7.38rem;
  height: 11rem;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
}
.paper-back {
  position: absolute;
  background: url('../assets/5/paper_back.png') no-repeat;
  background-size: 100% 100%;
  width: 5.55rem;
  height: 8.03rem;
  top: 1.9rem;
  left: 1.19rem;
}
.m-mail-open img {
  width: 100%;
  height: 100%;
  display: block;
}
</style>
